﻿// <copyright file="ChromeBrowserLanguagePreference.cs" company="None">(c) Guy Smith-Ferrier, 2012</copyright>

namespace Browser
{
    using System;
    using System.IO;

    /// <summary>
    /// ChromeBrowserLanguagePreference is a class to get and/or set the Chrome Language Preference
    /// </summary>
    /// <remarks>Chrome must not be running in order to set the language preference (because Chrome does not re-read its settings and it writes them
    /// back out when it closes thereby overwriting the changes made by this class).</remarks>
    public class ChromeBrowserLanguagePreference : IBrowserLanguagePreference
    {
        /// <summary>
        /// Gets or sets the browser's language preferences
        /// </summary>
        public string LanguagePreferences
        {
            get
            {
                string json = this.JsonContents;

                // Look for an entry like this:-
                // {
                //    "intl": {
                //      "accept_languages": "fr-FR,fr,en-GB,en,en-US"
                //   }, etc.
                int index = json.IndexOf(@"""accept_languages"":");
                if (index == -1)
                {
                    return null;
                }

                string acceptLanguages = json.Substring(index + 21);
                int closingQuote = acceptLanguages.IndexOf(@"""");
                if (closingQuote == -1)
                {
                    return null;
                }

                return acceptLanguages.Substring(0, closingQuote);
            }

            set
            {
                string json = this.JsonContents;

                int index = json.IndexOf(@"""accept_languages"":");
                if (index > -1)
                {
                    string jsonBeforeAcceptLanguages = json.Substring(0, index + 21);
                    string jsonAfterAcceptLanguages = json.Substring(index + 21);
                    int closingQuote = jsonAfterAcceptLanguages.IndexOf(@"""");
                    if (closingQuote > -1)
                    {
                        jsonAfterAcceptLanguages = jsonAfterAcceptLanguages.Substring(closingQuote);

                        json = jsonBeforeAcceptLanguages + value + jsonAfterAcceptLanguages;

                        this.JsonContents = json;
                    }
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether the browser exists on this computer
        /// </summary>
        public bool BrowserExists
        {
            get
            {
                return Directory.Exists(this.ChromePath);
            }
        }

        /// <summary>
        /// Gets the file path to the Chrome browser's data
        /// </summary>
        private string ChromePath
        {
            get
            {
                // C:\Users\<username>\Local Settings\Application Data\Google\Chrome
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Google\Chrome");
            }
        }

        /// <summary>
        /// Gets the file path to the Chrome browser's user data defaults
        /// </summary>
        private string UserDataDefaultPath
        {
            get
            {
                // C:\Users\<username>\Local Settings\Application Data\Google\Chrome\User Data\Default
                return Path.Combine(this.ChromePath, @"User Data\Default");
            }
        }

        /// <summary>
        /// Gets the file path to Chrome's Preferences file
        /// </summary>
        private string PreferencesPath
        {
            get
            {
                // C:\Users\<username>\Local Settings\Application Data\Google\Chrome\User Data\Default\Preferences
                return Path.Combine(this.UserDataDefaultPath, "Preferences");
            }
        }

        /// <summary>
        /// Gets or sets the contents of the Preferences file
        /// </summary>
        private string JsonContents
        {
            get
            {
                string jsonContents;
                using (TextReader reader = new StreamReader(this.PreferencesPath))
                {
                    jsonContents = reader.ReadToEnd();

                    reader.Close();
                }

                return jsonContents;
            }

            set
            {
                using (TextWriter writer = new StreamWriter(this.PreferencesPath))
                {
                    writer.Write(value);

                    writer.Close();
                }
            }
        }
    }
}
